<?php
/**
 * Created by PhpStorm.
 * User: xufei
 * Date: 2018/5/12
 * Time: 下午3:04
 */

class wxapi_ctl_home extends wxapi_response{
    public function __construct(&$app)
    {
        parent::__construct($app);
    }
    
    public function index()
    {
        $tagModel = app::get('desktop')->model('tag');        
        $sdf_tags = $tagModel->getList('tag_abbr,tag_id,tag_name,tag_link,link_switch,params',array('tag_type'=>'goods','show_small_tag'=>1),0, -1, 'display_order desc');
        if(!empty($sdf_tags)){
            foreach ($sdf_tags as &$val){
                if(!empty($val['params']['tag_image'])){
                    $val['tag_image'] = base_storager::image_path($val['params']['tag_image'],'l');
                }else{
                    $val['tag_image'] =array();
                }
                unset($val['params']);
            }
        }
        
        $columns = app::get('site')->model('smallprogram_columns');
        $columnsList = $columns->getlist('*',array('is_open'=>'true'),$offset = 0, $limit = -1, 'display_order desc');
        array_walk($columnsList, function (&$val){
            $val['disabled'] = $val['disabled'] == 'true' ? 1 : 0;
            $val['images'] = base_storager::image_path($val['images']);
            if(strpos($val['custom_url'],'cateCode') ||strpos($val['custom_url'],'tagId') ){
                $val['custom_url'] = $val['custom_url'];
            }else{
                $val['custom_url'] = $val['custom_url'].'&cateCode';
            }

        });
        $data['columns'] = $columnsList;

/*        $objCat = app::get('b2c')->model('goods_cat');
        $childCategoryList = $objCat->getList('*', array('spec_image|notin' => NULL,'is_small'=>'true' ), $offset = 0, $limit = 8, 'p_order ASC');
        $childCategoryArr = array();
        if (!empty($childCategoryList)){
            foreach($childCategoryList as $category)
            {
                $childCategoryLists = array();
                $childCategory =  array();
                $childCategory['code'] = $category['cat_id'];
                $childCategory['type'] = 'cat';
                $childCategory['name'] = $category['cat_name'];
                $childCategory['thumLogo'] = base_storager::image_path($category['spec_image']);
                $childLists = $objCat->getList('*', array('parent_id' => $category['cat_id']), $offset = 0, $limit = -1, 'p_order ASC');
                if (!empty($childLists)){
                    foreach ($childLists as $val) {
                        $childCategoryLists['code'] = $val['cat_id'];
                        $childCategoryLists['name'] = $val['cat_name'];
                        $childCategoryLists['cat_pid'] = $category['cat_id'];
                        $childCategoryLists['thumLogo'] = base_storager::image_path($val['spec_image']);
                        $childCategory['thirdCategoryList'][] = $childCategoryLists;
                    }
                }

                $childCategoryArr[] = $childCategory;
            }
        }*/

        $childCategoryArr = $this->__tempCategory();
        $data['category'] = $childCategoryArr;
        $data['tag'] = $sdf_tags;
        $data['items'] = $this->get_tag_goods();
        $return = [
            'result' => '',
            'data' => $data,
            'code' =>  0,
        ];
        return self::return_json($return);
    }
    
    public function get_tag_goods()
    {
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $size = $_GET['size'] ? $_GET['size'] : 7;
        $tagModel = app::get('desktop')->model('tag');
        $tagData = array();
        $sdf_tags = $tagModel->getList('tag_abbr,tag_id,tag_name,tag_link,link_switch,params',array('tag_type'=>'goods','show_small_tag_goods'=>1),($page -1) * $size, $size, 'display_order DESC');
        if (!empty($sdf_tags)){
            foreach ($sdf_tags as $key => &$tag){
                $goodsList = $this->__getTagGoods($tag['tag_id']);

                if (empty($goodsList)){
                    continue;
                }
                if(!empty($tag['params']['tag_image'])){
                    $tag['params']['tag_image'] = base_storager::image_path($tag['params']['tag_image'],'l');
                }

                $tag['sub_title'] = '';
                if($tag['tag_abbr'] && strstr($tag['tag_abbr'], '#')){
                    list($tag['tag_abbr'], $tag['sub_title']) = explode('#', $tag['tag_abbr']);
                }

                $tagData[$key] = $tag;
                $tagData[$key]['goodsList'] = $goodsList;
            }
        }
        
        return array_values($tagData);
    }

    public function getLabelGoods()
    {
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $size = $_GET['size'] ? $_GET['size'] : 7;
        $tagModel = app::get('desktop')->model('tag');
        $tagData = array();
        $sdf_tags = $tagModel->getList('*',array('tag_type'=>'goods','show_small_tag_goods'=>1),($page -1) * $size, $size, 'display_order DESC');
        if (!empty($sdf_tags)){
            foreach ($sdf_tags as $key => &$tag){
                $goodsList = $this->__getTagGoods($tag['tag_id']);

                if (empty($goodsList)){
                    continue;
                }
                if(!empty($tag['params']['tag_image'])){
                    $tag['params']['tag_image'] = base_storager::image_path($tag['params']['tag_image'],'l');
                }

                $tag['sub_title'] = '';
                if($tag['tag_abbr'] && strstr($tag['tag_abbr'], '#')){
                    list($tag['tag_abbr'], $tag['sub_title']) = explode('#', $tag['tag_abbr']);
                }

                $tagData[$key] = $tag;
                $tagData[$key]['goodsList'] = $goodsList;
            }
        }
        $return = [
            'result' => '',
            'data' => array(
                'items'=>array_values($tagData),
            ),
            'code' =>  0,
        ];
        self::return_json($return);
    }

    /**
     * 获取标签的商品
     */
    private function __getTagGoods($tag_id)
    {
        $tagList = array();
        $goodsTagsModel = app::get('b2c')->model('goods_tags');
        if($tag_id){
            $tagList = $goodsTagsModel->getList('*', array('tags_id'=>$tag_id));
            $goodsModel = app::get('b2c')->model('goods');
            if($tagList){
                foreach ($tagList as &$val){
                    $goods = $goodsModel->getRow('*', array('goods_id'=>$val['goods_id']));
                    
                    $image_default_id = $val['image'] ? $val['image'] : $goods['image_default_id'];
                    
                    $val['name'] = $goods['name'];
                    $val['image_default_id'] = base_storager::image_path($image_default_id);
                    $val['price'] = $goods['price'] == 0 ? 0 : number_format($goods['price'], 2);
                    $val['mktprice'] = $goods['mktprice'] == 0 ? 0:number_format($goods['mktprice'], 2);
                }
            }
            $goods_count = count($tagList);
            //第一个和最后一个全屏宽度
            foreach($tagList as $k=>&$v){
                $v['show_size'] = '2';
                if ($goods_count % 2 == 0 &&  $goods_count == ($k +1)){
                    $v['show_size'] = '1';
                }
                $tagList[0]['show_size'] = '1';
            }
        }

        return $tagList;
    }

    /**
     * 首页分类
     */
    public function __tempCategory(){

        $navigator = app::get('wxapi')->getConf('wxapps.navigator');
        foreach ($navigator as $k=>$v) {
            $navigator[$k]['thumLogo'] = base_storager::image_path($v['thumLogo']);
        }
        return $navigator;
    }

    public function getLogo() {
        $logoM5id = app::get('wxapi')->getConf('wxapps.logo');
        $logoM5id && $logoM5id = base_storager::image_path($logoM5id);
        //return 'http://ecstore02.demo.shopex123.com/public/images/8b/9d/de/4765af8bc8f8ec263a83d0bb8d66e557ce412d9b.jpg?1516776408#w';
        return $logoM5id;
    }

    public function getLawsConfig()
    {
        $laws = app::get('wxapi')->getConf('wxapps.laws');
        $laws['effectivetime'] = date('Y年m月d日',strtotime($laws['effectivetime']));
        self::return_json($laws);
    }

}